package com.bestv.task;

import com.bestv.db.DbFactory;
import com.bestv.db.DbHelper;
import com.bestv.db.MysqlDb;
import com.bestv.handler.UpdateTopKeywordThread;
import com.bestv.util.DbConstant;
import com.bestv.util.GlobalSettings;
import org.apache.log4j.Logger;

import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * Created by charle-chen on 15/11/24.
 */
public class UpdateTopKeywordTask {

    private MysqlDb mysqlDb = null;
    private int limit = 0;
    private Logger logger = Logger.getLogger(getClass());

    public void updateTopkeyword() {
        int maxCount = 0;
        mysqlDb = (MysqlDb) DbFactory.getDbInstance(DbConstant.MYSQL_DB);

        logger.info("update info table begining ...");

        String sql = "select count(1) from top_keyword";
        try {
            Statement st = mysqlDb.getConn().createStatement();
            ResultSet rs = st.executeQuery(sql);
            if (rs.next()) {
                maxCount = rs.getInt(1);
                logger.info("the top_keyword table's size: " + maxCount);
            }
            rs.close();
        } catch (SQLException e) {
            logger.error("update top_keyword sql error");
            e.printStackTrace();
        } finally {
            mysqlDb.closeDb();
        }

        int threadNum = GlobalSettings.getInstance().getThreadNum();
        limit = (int) Math.ceil(maxCount / (threadNum * 1.0));
        for (int i = 0; i < threadNum; i++) {
            UpdateTopKeywordThread thread = new UpdateTopKeywordThread();
            thread.setLimit(limit);
            thread.setStart(i * limit);
            thread.start();
        }
    }

    public void deleteTopkeyword() {
        mysqlDb = (MysqlDb) DbFactory.getDbInstance(DbConstant.MYSQL_DB);
        try {
            String deleleSql = "delete from top_keyword where count=0";
            mysqlDb.getConn().createStatement().executeUpdate(deleleSql);
        } catch (Exception e) {
            logger.error("delete top_keyword sql error");
            e.printStackTrace();
        } finally {
            logger.info("delete top_keyword where count=0 success!");
            mysqlDb.closeDb();
        }
    }

    public void backUpTopkeyword(){
        mysqlDb = (MysqlDb) DbFactory.getDbInstance(DbConstant.MYSQL_DB);
        try{
            logger.info("back up top_keyword begining ...");
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            String createSql = "create table if not exists top_keyword_bak_"+df.format(new Date())+" like top_keyword";
            String deleteSql = "truncate top_keyword_bak_"+df.format(new Date());
            String backupSql = "insert into top_keyword_bak_"+df.format(new Date())+" select * from top_keyword";
            String[] sqls = {createSql,deleteSql,backupSql};
            DbHelper.executeBatch(mysqlDb,sqls);
        }catch (Exception e){
            logger.error("back up top_keyword sql error");
            e.printStackTrace();
        } finally {
            logger.info("back up top_keyword success");
            mysqlDb.closeDb();
        }

    }
}
